<template>
  <div class="shopcart_wrapper">
    <!-- 底部购物车-->
    <div class="shopcart">
      <div class="content">
        <!--左侧-->
        <div class="content_left" @click="toggleList">
          <!-- 图标处-->
          <div class="icon_cart">
            <div class="icon_font" :class="{'food_have': totalCount > 0}">
              <span class="iconfont">&#xe62a;</span>
            </div>
            <div class="food_num" v-show="totalCount > 0">{{totalCount}}</div>
          </div>
          <!--价格处 -->
          <div class="total_price" :class="{'food_have': totalPrice > 0}">
            ¥ {{totalPrice}}
          </div>
          <!--配送费处-->
          <div class="post_price">
            另需配送费 ¥ {{deliveryPrice}} 元
          </div>
        </div>
        <!-- 右侧 -->
        <div class="content_right">
          <div class="pay" :class="payClass">
            {{stillPrice}}
          </div>
        </div>
      </div>
      <!--<div class="ball_container">
        <transition-group name="listball">
          <div class="ball" v-for="(ball, index) in balls" v-show="ball.show" :key="index">
            &lt;!&ndash;<div class="inner"></div>&ndash;&gt;
          </div>
        </transition-group>
      </div>-->
      <transition name="slide">
        <div class="shopcart_list" v-show="listShow">
          <div class="list_header">
            <h1 class="shopcart_title">购物车</h1>
            <span class="shopcart_empty" @click="empty">清空</span>
          </div>
          <div class="list_content">
            <ul>
              <li class="select_foods" v-for="food in selectFoods">
                <span class="food_name">{{food.name}}</span>
                <div class="cartcontrol_wrapper fr">
                  <v-cartcontrol :food="food"></v-cartcontrol>
                </div>
                <div class="food_price fr">
                  <span>¥{{food.price * food.count}}</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>
    </div>
    <!--背景-->
    <transition name="fade">
      <div class="modal" v-show="listShow" @click="hideList">
      </div>
    </transition>
  </div>
</template>

<script>
  import cartcontrol from 'components/cartcontrol/cartcontrol'
  export default {
    data() {
      return {
        balls: [
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          },
          {
            show: false
          }
        ],
        dropBall: [],
        fold: true
      }
    },
    props: {
      deliveryPrice: Number,
      minPrice: Number,
      selectFoods: {
        type: Array,
        default() {
          return [

          ]
        }
      }
    },
    components: {
      'v-cartcontrol': cartcontrol
    },
    computed: {
      totalPrice() {
        let total = 0
        this.selectFoods.forEach((food) => {
          total += food.price * food.count
        })
        return total
      },
      totalCount() {
        let count = 0
        this.selectFoods.forEach((food) => {
          count += food.count
        })
        return count
      },
      stillPrice() {
        if(this.totalPrice === 0) {
          return `¥ ${this.minPrice} 元起送`
        }else if (this.totalPrice < this.minPrice) {
          let still = this.minPrice - this.totalPrice
          return `还差¥ ${still}元起送`
        }else {
          return '去结算'
        }
      },
      payClass () {
        if(this.totalPrice < this.minPrice) {
          return 'not-enough'
        }else {
          return 'enough'
        }
      },
      listShow() {
        if(!this.totalCount) {
          this.fold = true
          return false
        }
        let show = !this.fold
        return show
      }
    },
    methods: {
      /*drop(el) {
        console.log(el)
        this.balls.forEach((ball) => {
          if(!ball.show) {
            ball.show = true
            ball.el = el
            this.dropBall.push(ball)
            return
          }
        })
      },
      beforeEnter(el) {
        let count = this.balls.length
        while(count--) {
          let ball = this.balls[count]
          if(ball.show) {
            let rect = ball.el.getBoundingClientRect() /!*获取元素相对视口的位置 left top*!/

          }
        }
      },*/
      toggleList() {
        if(!this.totalCount) {
          return
        }
        this.fold = !this.fold
      },
      empty() {
        this.selectFoods.forEach((food) => {
          food.count = 0
        })
      },
      hideList() {
        this.fold = true
      }
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  .shopcart_wrapper {
    .shopcart {
      position: fixed;
      left: 0;
      bottom: 0;
      z-index: 50;
      width: 100%;
      height: 48px;
      .content {
        display: flex;
        background-color: #141d27;
        .content_left {
          flex: 1;
          font-size: 0;
          .icon_cart {
            display: inline-block;
            position: relative;
            top: -10px;
            margin: 0 12px;
            padding: 6px;
            width: 56px;
            height: 56px;
            background: #141d27;
            -webkit-border-radius: 50%;
            -moz-border-radius: 50%;
            border-radius: 50%;
            vertical-align: top;
            .icon_font {
              width: 100%;
              height: 100%;
              -webkit-border-radius: 50%;
              -moz-border-radius: 50%;
              border-radius: 50%;
              background-color: #222A34;
              text-align: center;
              .iconfont {
                font-size: 24px;
                color: #80858A;
                line-height: 42px;
              }
            }
            .food_have {
              background-color: #00A0DC;
              .iconfont {
                color: #fff;
              }
            }
            .food_num {
              position: absolute;
              top: 0;
              right: 0;
              padding: 0 9px;
              height: 16px;
              line-height: 16px;
              text-align: center;
              border-radius: 16px;
              font-size: 9px;
              font-weight: 700;
              color: #fff;
              background-color: #F01414;
              box-shadow: 0 4px 8px 0 rgba(0,0,0,0.4);
            }
          }
          .total_price, .post_price {
            display: inline-block;
            vertical-align: top;
            font-weight: 700;
            color: #919396;
            font-size: 16px;
            margin-top: 12px;
            line-height: 24px;
          }
          .total_price {
            padding-right: 12px;
            border-right: 1px solid #2D353D;
            &.food_have {
              color: #fff;
            }
          }
          .post_price {
            font-size: 12px;
            margin-left: 12px;
          }
        }
        .content_right {
          flex: 0 0 105px;
          width: 105px;
          .pay {
            height: 48px;
            line-height: 48px;
            text-align: center;
            font-size: 12px;
            font-weight: 700;
            color: #989B9D;
            &.not-enough {
              background-color: #2B333B;
            }
            &.enough {
              background-color: #00b43c;
              color: #fff;
              &:active {
                background-color: #51a26c;
              }
            }
          }
        }
      }
      .ball_container {
        .ball {
          position: fixed;
          left: 32px;
          bottom: 22px;
          z-index: 100;
          width: 16px;
          height: 16px;
          border-radius: 50%;
          background-color: rgb(0,160,220);
        }

      }
      .shopcart_list {
        position: absolute;
        bottom: 48px;
        left: 0;
        z-index: -1;
        width: 100%;
        .list_header {
          height: 40px;
          line-height: 40px;
          padding: 0 18px;
          font-size: 14px;
          background-color: #F3F5F7;
          border-bottom: 1px solid #DBDEE1;
          .shopcart_title {
            float: left;
          }
          .shopcart_empty {
            float: right;
            font-size: 12px;
            color: rgb(0,160,220);
            padding-left: 10px;
          }
        }
        .list_content {
          padding: 0 18px 18px;
          max-height: 217px;
          background-color: #fff;
          overflow: auto;
          -webkit-overflow-scrolling: touch;
          .select_foods {
            position: relative;
            height: 48px;
            border-bottom: 1px solid #ECEEF0;
            .food_name {
              display: inline-block;
              line-height: 48px;
              font-size: 14px;
              color: #182028;
            }
            .food_price {
              line-height: 48px;
              font-size: 14px;
              margin-right: 5px;
              font-weight: 700;
              color: #F01414;
            }
            .cartcontrol_wrapper {
              margin-top: 6px;
            }
          }
        }
      }
      .slide-enter-active, .slide-leave-active {
        transition: all 0.35s;
      }
      .slide-enter, .slide-leave-active {
        transform: translate3d(0, 100%, 0);
      }
    }
    .modal {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 30;
      -webkit-backdrop-filter: blur(10px);
      background-color: rgba(0, 0, 0, .75);
    }
    .fade-enter-active, .fade-leave-active {
      transition: all 0.35s;
    }
    .fade-enter, .fade-leave-active {
      opacity: 0;
    }

  }

</style>